function clean() {
    $('#topicTitleField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#topicMessageField').parent().removeClass('has-error has-feedback').find('span').remove();
}

function validate() {
    clean();
    var validator = {valid: false};
    var topicTitleField = $('#topicTitleField').val();
    if (!topicTitleField) {
        $('#topicTitleField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Username must have 1-70 characters!</span>').insertAfter('#topicTitleField');
        return validator;
    }
    if (topicTitleField.length > 100) {
        $('#topicTitleField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Username must have 1-70 characters!</span>').insertAfter('#topicTitleField');
        return validator;
    }


    var topicMessageField = $('#topicMessageField').val();
    if (!topicMessageField) {
        $('#topicMessageField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Username must have 1-70 characters!</span>').insertAfter('#topicMessageField');
        return validator;
    }
    if (topicMessageField.length > 700) {
        $('#topicMessageField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Username must have 1-70 characters!</span>').insertAfter('#topicMessageField');
        return validator;
    }
    validator["topicTitleField"] = topicTitleField;
    validator["topicMessageField"] = topicMessageField;
    validator["valid"] = true;
    return validator;
}



function addPostClick(eventObject) {
    var validator = validate();
    if (validator["valid"]) {
        sendNewTopic(validator["topicTitleField"], validator["topicMessageField"], function (data) {
            if (data["success"] !== -1) {
                getTopicWithId(data["success"], function(top) {
                    var html = '<a href="seetopic.html?id=' + top["id_topic"] + '" class="list-group-item">' +
                        '<div class="row">' +
                            '<div class="col-xs-9">' +
                                '<h4 class="list-group-item-heading">' + top["title"] +
                                    (top["posts_size"] !== 0 ? '<span class="badge">' + top["posts_size"] + '</span>' : '') +
                                '</h4>' +
                            '</div>' +
                            '<div class="col-xs-3">' +
                                top["created_date"] +
                            '</div>' +
                        '</div>' +
                        '<div class="row">' +
                            '<div class="col-xs-12"> ' +
                                '<p class="list-group-item-text">' + top["message"] + '</p> ' +
                           '</div> ' +
                        '</div> ' +
                        '<div class="row"> ' +
                            '<div class="col-xs-12"> ' +
                                top["name_user"] +
                            '</div> ' +
                        '</div> ' + 
                    '</a> ';
                    $("#topicsDiv").append(html);
                    $('#topicTitleField').val("");
                    $('#topicMessageField').val("");
                });
            } else {
            }
        });
    }
}


